package coolingPool

import (
	"gorm.io/gorm"
	"gorm.io/gorm/clause"
	"xkginweb/global"
	"xkginweb/model/entity/coolingPool"
	"xkginweb/model/entity/coolingPool/request"
)

// 定义coolingPool的service提供coolingPool的数据curd的操作

type CoolingPoolService struct{}

// @author: hcl
// @function: CreateCoolingPool
// @description: 创建冷池
// @param: coolingPool.QccCoolingPool
// @return: err error

func (service *CoolingPoolService) CreateCoolingPool(coolingPool *coolingPool.QccCoolingPool) (err error) {
	// 1： 获取数据的连接对象 如果执行成功err是nil，如果失败就把失败告诉
	err = global.KSD_DB.Create(coolingPool).Error
	return err
}

//@author: hcl
//@function: UpdateCoolingPool
//@description: 更新冷池
//@param: coolingPool.QccCoolingPool
//@return: err error

func (service *CoolingPoolService) UpdateCoolingPool(coolingPool *coolingPool.QccCoolingPool) (err error) {
	//err = global.KSD_DB.Save(coolingPool).Error
	err = global.KSD_DB.Model(coolingPool).Updates(coolingPool).Error
	return err
}

// @author: hcl
// @function: DeleteCoolingPool
// @description: 删除冷池
// @param: coolingPool.QccCoolingPool
// @return: err error

func (service *CoolingPoolService) DeleteCoolingPool(coolingPool *coolingPool.QccCoolingPool) (err error) {
	err = global.KSD_DB.Delete(&coolingPool).Error
	return err
}

// @author: hcl
// @function: DeleteCoolingPoolById
// @description: 根据ID删除冷池
// @param: uint
// @return: err error

func (service *CoolingPoolService) DeleteCoolingPoolById(id uint) (err error) {
	var coolingPool coolingPool.QccCoolingPool
	err = global.KSD_DB.Where("cooling_pool_id = ?", id).Delete(&coolingPool).Error
	return err
}

// @author: hcl
// @function: GetCoolingPool
// @description: 根据ID获取冷池信息
// @param: id uint
// @return: coolingPool *coolingPool.QccCoolingPool, err error

func (service *CoolingPoolService) GetCoolingPool(id uint) (coolingPool *coolingPool.QccCoolingPool, err error) {
	err = global.KSD_DB.Where("cooling_pool_id = ?", id).Preload("QccRoom", func(db *gorm.DB) *gorm.DB {
		return db.Table("qcc_room").Select("room_id,room_name,room_type")
	}).First(&coolingPool).Error
	return
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: LoadCoolingPoolPage
//@description: 分页获取冷池列表
//@param: sysUserAuthorityID string, info request.PageInfo
//@return: list interface{}, total int64, err error

func (service *CoolingPoolService) LoadCoolingPoolPage(info request.CoolingPoolPageInfo) (list interface{}, total int64, err error) {
	// 获取分页的参数信息
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)

	// 准备查询那个数据库表
	db := global.KSD_DB.Model(&coolingPool.QccCoolingPool{})

	// 准备切片帖子数组
	var CoolingPoolList []coolingPool.QccCoolingPool

	if info.RoomId != nil {
		db = db.Where("room_id = ?", info.RoomId)
	}

	// 加条件
	if info.Keyword != "" {
		db = db.Where("(cooling_pool_name like ?  )", "%"+info.Keyword+"%")
	}

	// 排序默时间降序降序
	db = db.Order("created_at")

	// 查询中枢
	err = db.Count(&total).Error
	if err != nil {
		return CoolingPoolList, total, err
	} else {
		// 执行查询
		err = db.Limit(limit).Offset(offset).Preload("QccRoom", func(db *gorm.DB) *gorm.DB {
			return db.Table("qcc_room").Select("room_id,room_name,room_type")
		}).Find(&CoolingPoolList).Error
	}

	// 结果返回
	return CoolingPoolList, total, err
}

// 查询房间已绑定的冷池

func (service *CoolingPoolService) SelectRoomPool(roomId uint) (info []*coolingPool.QccCoolingPool, err error) {
	err = global.KSD_DB.Select("t2.* ").Table("qcc_room t1,qcc_cooling_pool t2").
		Where("t1.room_id = ? AND t1.room_id = t2.room_id and t2.is_deleted = 0", roomId).Scan(&info).Error
	return info, err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: LoadCoolingPool
//@description: 获取冷池列表
//@param: sysUserAuthorityID string, info request.PageInfo
//@return: list interface{}, err error

func (service *CoolingPoolService) LoadCoolingPool(info request.CoolingPoolInfo) (list []*coolingPool.QccCoolingPoolList, err error) {

	// 准备查询那个数据库表
	db := global.KSD_DB.Model(&coolingPool.QccCoolingPool{})

	// 准备切片帖子数组
	list = make([]*coolingPool.QccCoolingPoolList, 1, 1)

	if info.RoomId != nil {
		db = db.Where("room_id = ?", info.RoomId)
	}

	// 加条件
	if info.Keyword != "" {
		db = db.Where("(cooling_pool_name like ?  )", "%"+info.Keyword+"%")
	}

	// 排序默时间降序降序
	db = db.Order("created_at")

	// 执行查询
	err = db.Table("qcc_cooling_pool").Find(&list).Error

	// 结果返回
	return list, err
}

// 房间绑定冷池

func (service *CoolingPoolService) UpdateRoomPools(pools []*coolingPool.QccCoolingPoolList) (err error) {
	// 事务加持
	tx := global.KSD_DB.Begin()

	// 开始批量修改冷池的房间号
	err = tx.Clauses(clause.OnConflict{
		Columns:   []clause.Column{{Name: "cooling_pool_id"}},
		DoUpdates: clause.AssignmentColumns([]string{"room_id"}),
	}).Table("qcc_cooling_pool").Omit("created_at", "updated_at", "is_deleted", "cooling_pool_name", "type").Create(&pools).Error

	if err != nil {
		tx.Rollback()
		return err
	}

	return tx.Commit().Error
}
